package com.example.yimoopassport;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.content.Context;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.CaptureActivity;

import java.security.GeneralSecurityException;

public class MainActivity extends AppCompatActivity {
    static boolean isfp = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences sharedPreferences = getSharedPreferences("data", Context .MODE_PRIVATE);
        String keyID=sharedPreferences.getString("keyID","");
        String fpS=sharedPreferences.getString("fpS","");
        if(keyID.equals("")){
            Intent intent=new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(this,WelcomeActivity.class);
            startActivity(intent);
        }else{
            Switch s = (Switch)findViewById(R.id.fingerps);
          if(!isfp&&fpS.equals("on")){


                Intent intent=new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClass(this,LoginActivity.class);
                startActivity(intent);
          }
          if(fpS.equals("on")){
              s.setChecked(true);
          }else{
              s.setChecked(false);
          }

            mHandler.postDelayed(r, 100);
        }

    }
    Handler mHandler = new Handler();
    Runnable r = new Runnable() {
        @Override
        public void run()  {
            TwoFactorAuthUtil f = new TwoFactorAuthUtil();
            SharedPreferences sharedPreferences = getSharedPreferences("data", Context .MODE_PRIVATE);
            String keyID=sharedPreferences.getString("keyID","");
            try {
                String code = f.generateCurrentNumber(keyID);
                TextView codeview = (TextView)findViewById(R.id.Code);
                codeview.setText(code);
                long diff = TwoFactorAuthUtil.TIME_STEP_SECONDS - ((System.currentTimeMillis() / 1000) % TwoFactorAuthUtil.TIME_STEP_SECONDS);
                ProgressBar nexttime=(ProgressBar)findViewById(R.id.NextTime);
                nexttime.setProgress((int)diff);
                if(diff<=5){
                    codeview.setTextColor(Color.parseColor("#ff6347"));
                }else{
                    codeview.setTextColor(Color.parseColor("#008577"));
                }
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            }
            mHandler.postDelayed(this, 1000);
        }
    };
    public void onChangeFpS(View view){
        boolean isChecked;
        isChecked=((Switch)findViewById(R.id.fingerps)).isChecked();
        SharedPreferences sharedPreferences= getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if(isChecked){
            editor.putString("fpS","on");
        }else{
            editor.putString("fpS","off");
        }

        editor.commit();

    }

}
